package com.cdwh.joylife.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cdwh.joylife.R;
import com.cdwh.joylife.utils.UiUtils;


public class TitleBar extends RelativeLayout {

    private int backIcon;
    private String backText;
    private float backTextSize;
    private int backTextColor;
    private String titleText;
    private float titleTextSize;
    private int titleTextColor;
    private String rightText;
    private float rightTextSize;
    private int rightTextColor;
    private TextView mTextView;
    private ImageButton mImageViewBack;
    //    private TextView mTextViewBack;
    private TextView mTextViewRigth;
    //    private AutoLinearLayout autoLinearLayoutback;
    private int imageId = 0;

    private boolean backIconGone;
    private int titleColor;
    private RelativeLayout autoRelativeLayout;

    public TitleBar(Context context) {
        super(context);
    }


    public TitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        //加载视图的布局
        LayoutInflater.from(context).inflate(R.layout.title_bar, this, true);

        //加载自定义的属性
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.title);
        titleText = a.getString(R.styleable.title_titleText);
        rightText = a.getString(R.styleable.title_rightText);
        backText = a.getString(R.styleable.title_backText);
        titleTextColor = a.getColor(R.styleable.title_titleTextColor, Color.WHITE);
        titleColor = a.getColor(R.styleable.title_titleColor, UiUtils.getColor(R.color.colorPrimary));
        titleTextSize = a.getDimension(R.styleable.title_titleTextSize, 20f);
        imageId = a.getResourceId(R.styleable.title_backIcon, R.drawable.ic_action_navigation_arrow_back_inverted);
        backIconGone = a.getBoolean(R.styleable.title_bactIconGone, true);

        //回收资源，这一句必须调用
        a.recycle();
    }

    /**
     * 此方法会在所有的控件都从xml文件中加载完成后调用
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        //获取子控件
        mTextView = (TextView) findViewById(R.id.textViewTitle);
        mImageViewBack = (ImageButton) findViewById(R.id.imageViewBack);
//        autoLinearLayoutback = (AutoLinearLayout) findViewById(R.id.autoLinearLayoutback);
//        mTextViewBack = (TextView) findViewById(R.id.textViewBack);
        mTextViewRigth = (TextView) findViewById(R.id.textViewRigth);
        autoRelativeLayout = (RelativeLayout) findViewById(R.id.autoRelativeLayout);
        //将从资源文件中加载的属性设置给子控件
//        if (!TextUtils.isEmpty(titleText))
        setPageTitleText(titleText);
        setPageTitleTextColor(titleTextColor);
        setPageTitleTextSize(titleTextSize);
//        setPageBackText(backText);
        setPageBackIcon(imageId);
        setPageRightText(rightText);
        setTitleColor(titleColor);


    }

    public View getRightTextView() {
        return mTextViewRigth;
    }

//    public void setPageBackText(String text) {
//        mTextViewBack.setText(text);
//    }

    public void setPageBackIconGone(boolean isVisiblility) {
        if (isVisiblility) {
            mImageViewBack.setVisibility(GONE);
        } else {
            mImageViewBack.setVisibility(VISIBLE);
        }
    }

    public void setTitleColor(int color) {
        autoRelativeLayout.setBackgroundColor(color);
    }

    public void setPageRightText(String text) {
        mTextViewRigth.setText(text);
    }

    public void setPageBackIcon(int id) {
        mImageViewBack.setImageResource(id);
    }

    /**
     * 设置标题文字
     *
     * @param text
     */
    public void setPageTitleText(String text) {
        mTextView.setText(text);
    }

    public void setRightText(String text) {
        mTextView.setText(text);
    }

    /**
     * 设置标题文字颜色
     *
     * @param color
     */
    public void setPageTitleTextColor(int color) {
        mTextView.setTextColor(color);
    }

    /**
     * 设置标题文字大小
     *
     * @param size
     */
    public void setPageTitleTextSize(float size) {
        mTextView.setTextSize(size);
    }

    private OnClickListener onBackClickListener;

    public void setRihtTextClickable(boolean clickable) {
        mTextViewRigth.setClickable(clickable);
    }

    public void setOnRightClickListener(final OnClickListener listener) {

//            autoLinearLayoutback.setOnClickListener(onBackClickListener);
        mTextViewRigth.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(view);
            }
        });
    }

    private Activity mActivity;

    public void init(Activity activity) {
        mActivity = activity;
        onBackClickListener = new OnClickListener() {
            @Override
            public void onClick(View view) {
                mActivity.finish();
            }
        };
        mImageViewBack.setOnClickListener(onBackClickListener);
    }
}
